package com.tss.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tss.mapper.AnswerMapper;
import com.tss.pojo.Answer;
import com.tss.pojo.AnswerExample;
import com.tss.pojo.AnswerExample.Criteria;
import com.tss.service.AnswerService;

@Service
public class AnswerServiceImpl implements AnswerService{

	@Autowired
	private AnswerMapper answerMapper;
	
	@Override
	public boolean addAnswer(Answer answer) {
		if(answer==null){
			return false;
		}
		answer.setValidate(true);
		answerMapper.insert(answer);
		return true;
	}

	@Override
	public Integer countAnswer(Answer answer) {
		if(answer==null){
			return null;
		}
		AnswerExample example = new AnswerExample();
		Criteria criteria = example.createCriteria();
		criteria.andQuestionIdEqualTo(answer.getQuestionId());
		criteria.andOptionenIdEqualTo(answer.getOptionenId());
		criteria.andValidateEqualTo(true);
		return answerMapper.countByExample(example);
	}

	@Override
	public List<String> listAnswer(Answer answer) {
		List<String> list = new ArrayList<String>();
		if(answer==null||answer.getQuestionId()==null){
			return null;
		}
		AnswerExample example = new AnswerExample();
		Criteria criteria = example.createCriteria();
		criteria.andQuestionIdEqualTo(answer.getQuestionId());
		criteria.andValidateEqualTo(true);
		List<Answer> answers = answerMapper.selectByExample(example);
		if(answers.isEmpty()){
			return null;
		}
		for(Answer a : answers){
			list.add(a.getContent());
		}
		return list;
	}

}
